/*
* @Author: xiaobing00008
* @Date:   2018-10-09 10:33:57
* @Last Modified by:   xiaobing00008
* @Last Modified time: 2018-10-09 10:34:31
*/
import { createStore } from 'redux'
//注意最好不要state+1这种直接操作state
function counter(state=0,action) {
    switch (action.type) {
        case '加机关枪':
            return state+1
        case '减机关枪':
            return state-1
        default:
            return 10
   }
}

const store = createStore(counter)
//获取状态
const init = store.getState(store)
console.log('初始机关枪数为'+init)

function listener() {
    const count = store.getState(store)
    console.log(`现在有${count}把机关枪`)
}

//订阅监听listener变化
store.subscribe(listener)

//操作
store.dispatch({
    type:'加机关枪'
})

store.dispatch({
    type:'加机关枪'
})

store.dispatch({
    type:'减机关枪'
})